{% extends "shop/base.html" %}

{% load static %}

{% block title %}
你的购物车
{% endblock %}

{% block content %}
<h1>你的购物车</h1>
<table class="cart">
    <thead>
        <tr>
            <th>图片</th>
            <th>产品</th>
            <th>数量</th>
            <th>删除</th>
            <th>单价</th>
            <th>价格</th>
        </tr>
    </thead>

    <tbody>
        {% for item in cart %}
        {% with product=item.product %}
        <tr>
            <td>
                <a href="{{ product.get_absolute_url }}">
                    <img src="{% if product.image %}{{ product.image.url }}{% else %}{% static "img/no_image.png" %}{% endif %}">
                </a>
            </td>
            <td>{{ product.name }}</td>
            <td>
                <form action="{% url 'cart:cart_add' product.id %}" method="post">
                    {{ item.update_quantity_form.quantity }}
                    {{ item.update_quantity_form.update }}
                    <input type="submit" value='更新'>
                    {% csrf_token %}
                </form>
            </td>
            <td><a href="{% url "cart:cart_remove" product.id %}">删除</a></td>
            <td class="num">${{ item.price }}</td>
            <td class="num">${{ item.total_price }}</td>
        </tr>

        {% endwith %}
        {% endfor %}
        {% if cart.coupon %}
            <tr class="subtotal">
                <td>合计</td>
                <td colspan="4"></td>
                <td class="num">${{ cart.get_total_price }}</td>
            </tr>
            <tr>
            <td>
                "{{ cart.coupon.code }}" coupon
                ({{ cart.coupon.discount }}% off)
                </td>
                <td colspan="4"></td>
                <td class="num neg">
                - ${{ cart.get_discount|floatformat:"2" }}
                </td>
            </tr>
        {% endif %}
        <tr class="total">
            <td>总计</td>
            <td colspan="4"></td>
            <td class="num">
            ${{ cart.get_total_price_after_discount|floatformat:"2" }}
            </td>
        </tr>
    </tbody>
</table>

<p>应用一个优惠券:</p>
<form action="{% url "coupons:coupon_apply" %}" method="post">
    {{ coupon_apply_form }}
    <input type="submit" value="Apply">
    {% csrf_token %}
</form>


<p class="text-right">
    <a href="{% url "shop:product_list" %}" class="button light">继续购物</a>
    <a href="{% url "orders:order_create" %}" class="button">提交订单</a>

</p>
{% endblock %}